import socket
import msgpasser;
from msg import *;
import sys;
import os;
from CheckIn import check_in
from CheckOut import check_out
from QueryUsers import query_users

name = sys.argv[1]
print name
mp = msgpasser.messagePasser('demo.yaml', name,'l')
msgpasser.mp = mp

srvSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srvSock.bind(('', 12345))
srvSock.listen(5)

while 1:
    cliSock, cliAddr = srvSock.accept()
    msg = cliSock.recv(1024)
    print "Message = %s" % msg
    t = msg.split(";");
    if(t[0] == "check_in"):
        print "check in!"
        #check_in(uname, cookie, status, availability);
        res = check_in(mp, t[1], t[2], t[3], t[4])
        if(res == True):
            cliSock.send("ACK");
        else:
            cliSock.send("NACK");
    elif(t[0] == "check_out"):
        res = check_out(mp, t[1])
        cliSock.send(ret)
    elif(t[0] == "directions"):
        cliSock.send("1. Walk to the end of the hallway and make a left.\n2. Make the third right.\n3. You will arrive at your destination on the left.")
    elif(t[0] == "query_users"):
        ret = query_users()
        print ret
        cliSock.send(ret)
    elif(t[0] == "get_message"):
        cliSock.send("12-14-2012 1:20:17.207,asantell,Idont know what to write for a message;12-14-2012 1:20:16:891,jimmyd,here is another message;")
    print t
    #cliSock.send(msg)
    cliSock.close()
